home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Tool Chest / Development Tools & Languages / Dylan Related / Marlais / Marlais 0.5.9-portable sources / keyword.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-15  |  3.3 KB  |  144 lines  |  [TEXT/ttxt]

  1. /*
  2.  
  3.    keyword.c
  4.  
  5.    This software is free software; you can redistribute it and/or
  6.    modify it under the terms of the GNU Library General Public
  7.    License as published by the Free Software Foundation; either
  8.    version 2 of the License, or (at your option) any later version.
  9.  
  10.    This software is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.    Library General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU Library General Public
  16.    License along with this software; if not, write to the Free
  17.    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.    Original copyright notice follows:
  20.  
  21.    Copyright, 1993, Brent Benson.  All Rights Reserved.
  22.    0.4 & 0.5 Revisions Copyright 1994, Joseph N. Wilson.  All Rights Reserved.
  23.  
  24.    Permission to use, copy, and modify this software and its
  25.    documentation is hereby granted only under the following terms and
  26.    conditions.  Both the above copyright notice and this permission
  27.    notice must appear in all copies of the software, derivative works
  28.    or modified version, and both notices must appear in supporting
  29.    documentation.  Users of this software agree to the terms and
  30.    conditions set forth in this notice.
  31.  
  32.  */
  33.  
  34. #include <string.h>
  35.  
  36. #include "keyword.h"
  37.  
  38. #include "bytestring.h"
  39. #include "list.h"
  40. #include "prim.h"
  41. #include "symbol.h"
  42.  
  43. /* primitives */
  44.  
  45. static Object symbol_to_string (Object sym);
  46. static Object string_to_symbol (Object str);
  47.  
  48. static struct primitive keyword_prims[] =
  49. {
  50.     {"%keyword->symbol", prim_1, keyword_to_symbol},
  51.     {"%symbol->keyword", prim_1, symbol_to_keyword},
  52.     {"%symbol->string", prim_1, symbol_to_string},
  53.     {"%string->symbol", prim_1, string_to_symbol},
  54. };
  55.  
  56. /* function definitions */
  57.  
  58. void
  59. init_keyword_prims (void)
  60. {
  61.     int num;
  62.  
  63.     num = sizeof (keyword_prims) / sizeof (struct primitive);
  64.  
  65.     init_prims (num, keyword_prims);
  66. }
  67.  
  68. /* take a "foo:" keyword and return the symbol "foo" 
  69.  */
  70. Object
  71. keyword_to_symbol (Object keyword)
  72. {
  73.     char name[MAX_SYMBOL_SIZE];
  74.     int size;
  75.  
  76.     strcpy (name, KEYNAME (keyword));
  77.     size = strlen (name);
  78.     name[size - 1] = '\0';
  79.     return (make_symbol (name));
  80. }
  81.  
  82. Object
  83. symbol_to_keyword (Object symbol)
  84. {
  85.     char name[MAX_SYMBOL_SIZE];
  86.     int size;
  87.  
  88.     strcpy (name, SYMBOLNAME (symbol));
  89.     strcat (name, ":");
  90.     return (make_keyword (name));
  91. }
  92.  
  93. /* Return the value of a keyword from a list, e.g., 
  94.  
  95.    (find-keyword foo: '(wagga fooga :bar 3 :foo 6 :baz 8)) => 6
  96.  
  97.  */
  98. Object
  99. find_keyword_val (Object keyword, Object lst)
  100. {
  101.     if (!LISTP (lst)) {
  102.     return (NULL);
  103.     }
  104.     while (!NULLP (lst)) {
  105.     if (CAR (lst) == keyword && !NULLP (CDR (lst))) {
  106.         return SECOND (lst);
  107.     }
  108.     lst = CDR (lst);
  109.     }
  110.     return (NULL);
  111. }
  112.  
  113. /* Return all keywords in the given list.
  114.  */
  115. Object
  116. all_keywords (Object lst)
  117. {
  118.     Object l, keywords;
  119.  
  120.     l = lst;
  121.     keywords = make_empty_list ();
  122.     while (!NULLP (l)) {
  123.     if (KEYWORDP (CAR (l))) {
  124.         keywords = cons (CAR (l), keywords);
  125.     }
  126.     l = CDR (l);
  127.     }
  128.     return (keywords);
  129. }
  130.  
  131. /* locals */
  132.  
  133. static Object
  134. symbol_to_string (Object sym)
  135. {
  136.     return (make_byte_string (SYMBOLNAME (sym)));
  137. }
  138.  
  139. static Object
  140. string_to_symbol (Object str)
  141. {
  142.     return (make_symbol (BYTESTRVAL (str)));
  143. }
  144.